package com.nuoniu.sibanyun.service.crm.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.crm.CrmPhrase;
import com.nuoniu.sibanyun.entity.dto.CrmPhraseDto;
import com.nuoniu.sibanyun.mapper.crm.CrmPhraseMapper;
import com.nuoniu.sibanyun.service.crm.ICrmPhraseService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 快捷短语 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-07-16
 */
@Service
public class CrmPhraseServiceImpl extends ServiceImpl<CrmPhraseMapper, CrmPhrase> implements ICrmPhraseService {

    @Override
    public List<CrmPhrase> listCrmPhrase(CrmPhraseDto crmPhraseDto) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<CrmPhrase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_user_id",userInfo.getUserId());
        queryWrapper.eq("status",0);
        if(StringUtils.isNoneBlank(crmPhraseDto.getContentVal())){
            queryWrapper.like("content_val",crmPhraseDto.getContentVal());
        }
        queryWrapper.orderByAsc("sort");
        List<CrmPhrase> list = list(queryWrapper);
        return list;
    }

    @Override
    public void batchDelete(String ids) {
        if(StringUtils.isBlank(ids)){
            throw new NuoNiuException("参数错误,请选择你要删除的记录");
        }
        String[] split = ids.split(",");
        if(split.length == 0){
            throw new NuoNiuException("参数错误,请选择你要删除的记录");
        }
        UserInfo userInfo = UserThreadLocal.get();
        List<String> strings = Arrays.asList(split);
        UpdateWrapper<CrmPhrase> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id",strings);
        updateWrapper.eq("create_user_id",userInfo.getUserId());
        updateWrapper.set("status",1);

        removeByIds(strings);
    }

    @Override
    public void batchSave(List<CrmPhraseDto> crmPhraseDtos) {
        if(null == crmPhraseDtos){
            throw new NuoNiuException("参数错误");
        }
        UserInfo userInfo = UserThreadLocal.get();
        LocalDateTime now = LocalDateTime.now();
        List<CrmPhrase> crmPhraseList = new ArrayList<>();
        for ( CrmPhraseDto crmPhraseDto : crmPhraseDtos ) {
            CrmPhrase crmPhrase = new CrmPhrase();
            BeanUtils.copyProperties(crmPhraseDto,crmPhrase);
            crmPhrase.setStatus(null);
            if(null != crmPhrase.getId()){
                crmPhrase.setUpdateUserId(userInfo.getUserId());
                crmPhrase.setUpdateUserName(userInfo.getRealName());
                crmPhrase.setUpdateDate(now);
            }
            crmPhrase.setCreateUserId(userInfo.getUserId());
            crmPhrase.setCreateUserName(userInfo.getRealName());
            crmPhrase.setCreateDate(now);
            crmPhraseList.add(crmPhrase);
        }
        if(crmPhraseList.size() > 0){
            saveOrUpdateBatch(crmPhraseList);
        }
    }
}
